﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.WorkOrder
{
    public partial class OperationList : Common.PageBase
    {

        public string ModifyUrl = "OperationModify.aspx?OPERATIONID={0}";

        T_WORKORDEROPERATION_BLL bll = new T_WORKORDEROPERATION_BLL();
        private T_WORKORDERTYPE_BLL workOrderTypeBll = new T_WORKORDERTYPE_BLL();
        private T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();

        private T_PERMISSION_BLL permissionBll = new T_PERMISSION_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.PageSize = StaticConstant.PageSize;
            PageHelper.SetPrePageUrl(this.Page);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlORDERTYPEIDBinding();
                ddlCURRENTSTATUSIDBinding();
                ddlNEXTSTATUSIDBinding();

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定


        /// <summary>
        ///	工单类型编号 下拉列表数据绑定
        /// </summary>
        private void ddlORDERTYPEIDBinding()
        {
            ddlORDERTYPEID.DataSource = workOrderTypeBll.GetCache_T_WORKORDERTYPE_List(this.Page);
            ddlORDERTYPEID.DataTextField = "OrderTypeName";
            ddlORDERTYPEID.DataValueField = "OrderTypeID";
            ddlORDERTYPEID.DataBind();
            ddlORDERTYPEID.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        ///	当前工单状态编号 下拉列表数据绑定
        /// </summary>
        private void ddlCURRENTSTATUSIDBinding()
        {
            ddlCURRENTSTATUSID.DataSource = workOrderStatusBll.GetSessionList(this.Page, ddlORDERTYPEID.SelectedValue.Trim());
            ddlCURRENTSTATUSID.DataTextField = "OrderStatusName";
            ddlCURRENTSTATUSID.DataValueField = "OrderStatusID";
            ddlCURRENTSTATUSID.DataBind();
            ddlCURRENTSTATUSID.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        ///	后顺状态编号 下拉列表数据绑定
        /// </summary>
        private void ddlNEXTSTATUSIDBinding()
        {
            ddlNEXTSTATUSID.DataSource = workOrderStatusBll.GetSessionList(this.Page, ddlORDERTYPEID.SelectedValue.Trim());
            ddlNEXTSTATUSID.DataTextField = "OrderStatusName";
            ddlNEXTSTATUSID.DataValueField = "OrderStatusID";
            ddlNEXTSTATUSID.DataBind();
            ddlNEXTSTATUSID.Items.Insert(0, new ListItem("", ""));
        }








        #endregion

        protected void ddlORDERTYPEID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCURRENTSTATUSIDBinding();
            ddlNEXTSTATUSIDBinding();
        }

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
            ddlORDERTYPEID_SelectedIndexChanged(null, null);
            ddlCURRENTSTATUSID.SelectedValue = PageHelper.GetQueryString(this.Page, "CURRENTSTATUSID");
            ddlNEXTSTATUSID.SelectedValue = PageHelper.GetQueryString(this.Page, "NEXTSTATUSID");
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["OPERATIONID"]))
            {
                sql.Append(" and OPERATIONID like '%" + Request.QueryString["OPERATIONID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OPERATIONNAME"]))
            {
                sql.Append(" and OPERATIONNAME like '%" + Request.QueryString["OPERATIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ORDERTYPEID"]))
            {
                sql.Append(" and ORDERTYPEID = '" + Request.QueryString["ORDERTYPEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ORDERTYPENAME"]))
            {
                sql.Append(" and ORDERTYPENAME like '%" + Request.QueryString["ORDERTYPENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CURRENTSTATUSID"]))
            {
                sql.Append(" and CURRENTSTATUSID = '" + Request.QueryString["CURRENTSTATUSID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CURRENTSTATUSNAME"]))
            {
                sql.Append(" and CURRENTSTATUSNAME like '%" + Request.QueryString["CURRENTSTATUSNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["NEXTSTATUSID"]))
            {
                sql.Append(" and NEXTSTATUSID = '" + Request.QueryString["NEXTSTATUSID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["NEXTSTATUSNAME"]))
            {
                sql.Append(" and NEXTSTATUSNAME like '%" + Request.QueryString["NEXTSTATUSNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ASSOCIATEDASSEMBLY"]))
            {
                sql.Append(" and ASSOCIATEDASSEMBLY like '%" + Request.QueryString["ASSOCIATEDASSEMBLY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ASSOCIATEDCLASS"]))
            {
                sql.Append(" and ASSOCIATEDCLASS like '%" + Request.QueryString["ASSOCIATEDCLASS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ASSOCIATEDMETHOD"]))
            {
                sql.Append(" and ASSOCIATEDMETHOD like '%" + Request.QueryString["ASSOCIATEDMETHOD"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISUPDATEINFO"]))
            {
                sql.Append(" and ISUPDATEINFO like '%" + Request.QueryString["ISUPDATEINFO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }

            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_WORKORDEROPERATION_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "OPERATIONID", "ORDERTYPEID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_WORKORDEROPERATION_Entity entity = new T_WORKORDEROPERATION_Entity();
            entity.OPERATIONID = StringHelper.ToLong((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            string _permissionCode = "";
            string _orderTypeId = (sender as GridView).DataKeys[e.RowIndex].Values["ORDERTYPEID"].ToString();
            switch ((WorkOrderType)Enum.Parse(typeof(WorkOrderType), _orderTypeId))
            {
                case WorkOrderType.咨询工单:
                    {
                        _permissionCode = "ConsultingCase" + (sender as GridView).DataKeys[e.RowIndex].Values[0].ToString();
                    }
                    break;
                case WorkOrderType.投诉工单:
                    {
                        _permissionCode = "ComplaintCase" + (sender as GridView).DataKeys[e.RowIndex].Values[0].ToString();
                    }
                    break;
                case WorkOrderType.报修工单:
                    {
                        _permissionCode = "RepairCase" + (sender as GridView).DataKeys[e.RowIndex].Values[0].ToString();
                    }
                    break;
            }


            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                permissionBll.Delete("PermissionCode", _permissionCode);
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "ISUPDATEINFO":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            temp = Enum.Parse(typeof(IsUpdateInfo), temp).ToString();
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
		
    }
}
